package com.example.pingze.service;

import com.example.pingze.entity.Creation;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.repository.CreationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class CreationService {
    @Autowired
    private CreationRepository repository;

    public Creation addCreation(Creation creation) {
        return repository.save(creation);
    }


    public List<Creation> getCreationByTheme(String theme) {
        return repository.findCreationByTheme(theme);
    }

    public Page getCreationList(Creation creation, Pageable pageable) {
        Specification<Creation> specification=new Specification<Creation>() {
            @Override
            public Predicate toPredicate(Root<Creation> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates=new ArrayList<>();
                if(!StringUtils.isEmpty(creation.getId())){
                    predicates.add(criteriaBuilder.equal(root.get("id"),creation.getId()));
                }
                if(!StringUtils.isEmpty(creation.getAccount())){
                    predicates.add(criteriaBuilder.equal(root.get("account"),creation.getAccount()));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page page = repository.findAll(specification,pageable);
        return page;
    }

    public Creation add(String nickname, String time, String theme, String title, String content, String imagePaths, int love, int comment, String account) {
        if(StringUtils.isEmpty(nickname)){
            throw new PingZeException("创作者昵称不能为空");
        }
        if(StringUtils.isEmpty(time)){
            throw new PingZeException("创作时间不能为空");
        }
        if(StringUtils.isEmpty(theme)){
            throw  new PingZeException("创作主题不能为空");
        }
        if(StringUtils.isEmpty(title)){
            throw  new PingZeException("创作标题不能为空");
        }
        if(StringUtils.isEmpty(content)){
            throw  new PingZeException("创作正文不能为空");
        }
        if(StringUtils.isEmpty(imagePaths)){
            throw  new PingZeException("背景图片不能为空");
        }
        if(love < 0){
            throw new PingZeException("点赞数不符合要求");
        }
        if(comment<0){
            throw new PingZeException("评论数不符合要求");
        }
        if(StringUtils.isEmpty(account)){
            throw  new PingZeException("创作者账号不能为空");
        }
        Creation add = new Creation();
        add.setId(UUID.randomUUID().toString());
        add.setNickname(nickname);
        add.setTime(time);
        add.setTheme(theme);
        add.setTitle(title);
        add.setContent(content);
        add.setImagePaths(imagePaths);
        add.setLove(love);
        add.setComment(comment);
        add.setAccount(account);
        repository.save(add);
        return add;
    }

    public Creation edit(String id, String nickname, String time, String theme,
                         String title, String content, String imagePaths,
                         int love, int comment, String account
    ) {
        Creation edit = repository.findCreationById(id);
        if(!edit.getNickname().equals(nickname)){
            edit.setNickname(nickname);
        }
        if(!edit.getTime().equals(time)){
            edit.setTime(time);
        }
        if(!edit.getTheme().equals(theme)){
            edit.setTheme(theme);
        }
        if(!edit.getTitle().equals(title)){
            edit.setTitle(title);
        }
        if(!edit.getContent().equals(content)){
            edit.setContent(content);
        }
        if(!edit.getImagePaths().equals(imagePaths)){
            edit.setImagePaths(imagePaths);
        }
        edit.setLove(love);
        edit.setComment(comment);
        if(!edit.getAccount().equals(account)){
            edit.setAccount(account);
        }
        repository.save(edit);
        return edit;
    }

    public Creation findCreationById(String id) {
        return repository.findCreationById(id);
    }

    public void deleteCreation(Creation creation) {
        repository.delete(creation);
    }

    // 获取数据库中最新的评论，并将评论中的imagePaths转换成base64编码格式，以集合转string的方式返回
    public List<Creation> getNewCreationByTheme(String theme) throws IOException {
        return repository.findNewCreationByTheme(theme);
    }

    // 获取最热评论
    public List<Creation> getHottestCreationByTheme(String theme) throws IOException {
        return repository.findHottestCreationByTheme(theme);
    }

    public void saveCount(Creation find) {
        repository.save(find);
    }

}
